package com.jingwu.example.service

import com.jingwu.example.pojo.entity.UserDO
import com.jingwu.example.repository.UserRepository
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import spock.lang.Specification

import static org.mockito.Mockito.when

/**
 * mockito init without spring container
 *
 * @author 菁芜
 * @since 2021/11/20 - 16:09
 */
class UserService3Test extends Specification {
    @Mock
    UserRepository repository
    @InjectMocks
    UserService service

    def setup() {
        MockitoAnnotations.initMocks(this)
    }

    def "test get User"() {
        given:
        def user1 = new UserDO(id: 1, username: "爆爆", age: 8)
        def user2 = new UserDO(id: 2, username: "金克丝", age: 16)

        and:
        when(repository.selectById(1)).thenReturn(user1)
        when(repository.selectById(2)).thenReturn(user2)

        when:
        def user = service.getUser(2)

        then:
        with(user) {
            user.username == "金克丝"
            user.age == 16
        }
    }
}
